package com.example.research.service;

import com.example.research.entity.VerticalProject;
import com.example.research.mapper.VerticalProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 纵向课题服务类
 */
@Service
@Transactional
public class VerticalProjectService {

    @Autowired
    private VerticalProjectMapper verticalProjectMapper;

    /**
     * 根据ID查询纵向课题
     */
    public VerticalProject findById(Long id) {
        return verticalProjectMapper.selectById(id);
    }

    /**
     * 查询所有纵向课题
     */
    public List<VerticalProject> findAll() {
        return verticalProjectMapper.selectAll();
    }

    /**
     * 根据用户ID查询纵向课题
     */
    public List<VerticalProject> findByUserId(Long userId) {
        return verticalProjectMapper.selectByUserId(userId);
    }

    /**
     * 根据审核状态查询纵向课题
     */
    public List<VerticalProject> findByAuditStatus(Integer auditStatus) {
        return verticalProjectMapper.selectByAuditStatus(auditStatus);
    }

    /**
     * 根据项目级别查询纵向课题
     */
    public List<VerticalProject> findByProjectLevel(String projectLevel) {
        return verticalProjectMapper.selectByProjectLevel(projectLevel);
    }

    /**
     * 根据时间范围查询纵向课题
     */
    public List<VerticalProject> findByDateRange(LocalDate startDate, LocalDate endDate) {
        return verticalProjectMapper.selectByDateRange(startDate, endDate);
    }

    /**
     * 根据条件查询纵向课题
     */
    public List<VerticalProject> findByConditions(Long userId, String projectLevel, 
                                                 Integer auditStatus, LocalDate startDate, LocalDate endDate) {
        return verticalProjectMapper.selectByConditions(userId, projectLevel, auditStatus, startDate, endDate);
    }

    /**
     * 创建纵向课题
     */
    public VerticalProject createProject(VerticalProject project) {
        // 设置默认审核状态为待审核
        if (project.getAuditStatus() == null) {
            project.setAuditStatus(0);
        }

        verticalProjectMapper.insert(project);
        return project;
    }

    /**
     * 更新纵向课题
     */
    public VerticalProject updateProject(VerticalProject project) {
        VerticalProject existingProject = verticalProjectMapper.selectById(project.getId());
        if (existingProject == null) {
            throw new RuntimeException("纵向课题不存在");
        }

        // 检查是否可以修改
        if (!existingProject.canModify()) {
            throw new RuntimeException("审核通过的课题不能修改");
        }

        // 检查权限：只能修改自己的课题
        if (!existingProject.getUserId().equals(project.getUserId())) {
            throw new RuntimeException("只能修改自己的课题");
        }

        verticalProjectMapper.update(project);
        return verticalProjectMapper.selectById(project.getId());
    }

    /**
     * 删除纵向课题
     */
    public void deleteProject(Long id, Long userId) {
        VerticalProject project = verticalProjectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException("纵向课题不存在");
        }

        // 检查是否可以删除
        if (!project.canModify()) {
            throw new RuntimeException("审核通过的课题不能删除");
        }

        // 检查权限：只能删除自己的课题
        if (!project.getUserId().equals(userId)) {
            throw new RuntimeException("只能删除自己的课题");
        }

        verticalProjectMapper.deleteById(id);
    }

    /**
     * 更新审核状态
     */
    public void updateAuditStatus(Long id, Integer auditStatus, String auditComment) {
        VerticalProject project = verticalProjectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException("纵向课题不存在");
        }

        verticalProjectMapper.updateAuditStatus(id, auditStatus, auditComment);
    }

    /**
     * 提交审核
     */
    public void submitForAudit(Long id, Long userId) {
        VerticalProject project = verticalProjectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException("纵向课题不存在");
        }

        // 检查权限
        if (!project.getUserId().equals(userId)) {
            throw new RuntimeException("只能提交自己的课题");
        }

        // 检查当前状态
        if (project.getAuditStatus() == 1) {
            throw new RuntimeException("课题已审核通过，无需重复提交");
        }

        // 更新为待审核状态
        verticalProjectMapper.updateAuditStatus(id, 0, null);
    }
}
